package com.skyz.wrap.http;

import android.app.Application;

import com.skyz.base.application.BaseApplication;
import com.skyz.wrap.manager.TokenManager;

import androidx.annotation.NonNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class UserTokenInterceptor implements Interceptor {
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request originalRequest = chain.request();
        Request.Builder builder = originalRequest.newBuilder();
        builder.method(originalRequest.method(), originalRequest.body());

        Application application = BaseApplication.getApplication();
        if (TokenManager.getInstance().isLogin(application)) {
            String token = TokenManager.getInstance().getToken(application);
            builder.header("token", token);
            builder.header("Authorization", "Bearer "+token);
        }
        Request request = builder.build();

        return chain.proceed(request);
    }
}

